{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0-alpha0\n",
      "sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)\n",
      "matplotlib 3.0.3\n",
      "numpy 1.16.2\n",
      "pandas 0.24.2\n",
      "sklearn 0.20.3\n",
      "tensorflow 2.0.0-alpha0\n",
      "tensorflow.python.keras.api._v2.keras 2.2.4-tf\n"
     ]
    }
   ],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sklearn\n",
    "import pandas as pd\n",
    "import os\n",
    "import sys\n",
    "import time\n",
    "import tensorflow as tf\n",
    "\n",
    "from tensorflow import keras\n",
    "\n",
    "print(tf.__version__)\n",
    "print(sys.version_info)\n",
    "for module in mpl, np, pd, sklearn, tf, keras:\n",
    "    print(module.__name__, module.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   survived     sex   age  n_siblings_spouses  parch     fare  class     deck  \\\n",
      "0         0    male  22.0                   1      0   7.2500  Third  unknown   \n",
      "1         1  female  38.0                   1      0  71.2833  First        C   \n",
      "2         1  female  26.0                   0      0   7.9250  Third  unknown   \n",
      "3         1  female  35.0                   1      0  53.1000  First        C   \n",
      "4         0    male  28.0                   0      0   8.4583  Third  unknown   \n",
      "\n",
      "   embark_town alone  \n",
      "0  Southampton     n  \n",
      "1    Cherbourg     n  \n",
      "2  Southampton     y  \n",
      "3  Southampton     n  \n",
      "4   Queenstown     y  \n",
      "   survived     sex   age  n_siblings_spouses  parch     fare   class  \\\n",
      "0         0    male  35.0                   0      0   8.0500   Third   \n",
      "1         0    male  54.0                   0      0  51.8625   First   \n",
      "2         1  female  58.0                   0      0  26.5500   First   \n",
      "3         1  female  55.0                   0      0  16.0000  Second   \n",
      "4         1    male  34.0                   0      0  13.0000  Second   \n",
      "\n",
      "      deck  embark_town alone  \n",
      "0  unknown  Southampton     y  \n",
      "1        E  Southampton     y  \n",
      "2        C  Southampton     y  \n",
      "3  unknown  Southampton     y  \n",
      "4        D  Southampton     y  \n"
     ]
    }
   ],
   "source": [
    "# https://storage.googleapis.com/tf-datasets/titanic/train.csv\n",
    "# https://storage.googleapis.com/tf-datasets/titanic/eval.csv\n",
    "train_file = \"./data/titanic/train.csv\"\n",
    "eval_file = \"./data/titanic/eval.csv\"\n",
    "\n",
    "train_df = pd.read_csv(train_file)\n",
    "eval_df = pd.read_csv(eval_file)\n",
    "\n",
    "print(train_df.head())\n",
    "print(eval_df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      sex   age  n_siblings_spouses  parch     fare  class     deck  \\\n",
      "0    male  22.0                   1      0   7.2500  Third  unknown   \n",
      "1  female  38.0                   1      0  71.2833  First        C   \n",
      "2  female  26.0                   0      0   7.9250  Third  unknown   \n",
      "3  female  35.0                   1      0  53.1000  First        C   \n",
      "4    male  28.0                   0      0   8.4583  Third  unknown   \n",
      "\n",
      "   embark_town alone  \n",
      "0  Southampton     n  \n",
      "1    Cherbourg     n  \n",
      "2  Southampton     y  \n",
      "3  Southampton     n  \n",
      "4   Queenstown     y  \n",
      "      sex   age  n_siblings_spouses  parch     fare   class     deck  \\\n",
      "0    male  35.0                   0      0   8.0500   Third  unknown   \n",
      "1    male  54.0                   0      0  51.8625   First        E   \n",
      "2  female  58.0                   0      0  26.5500   First        C   \n",
      "3  female  55.0                   0      0  16.0000  Second  unknown   \n",
      "4    male  34.0                   0      0  13.0000  Second        D   \n",
      "\n",
      "   embark_town alone  \n",
      "0  Southampton     y  \n",
      "1  Southampton     y  \n",
      "2  Southampton     y  \n",
      "3  Southampton     y  \n",
      "4  Southampton     y  \n",
      "0    0\n",
      "1    1\n",
      "2    1\n",
      "3    1\n",
      "4    0\n",
      "Name: survived, dtype: int64\n",
      "0    0\n",
      "1    0\n",
      "2    1\n",
      "3    1\n",
      "4    1\n",
      "Name: survived, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "y_train = train_df.pop('survived')\n",
    "y_eval = eval_df.pop('survived')\n",
    "\n",
    "print(train_df.head())\n",
    "print(eval_df.head())\n",
    "print(y_train.head())\n",
    "print(y_eval.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>n_siblings_spouses</th>\n",
       "      <th>parch</th>\n",
       "      <th>fare</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>627.000000</td>\n",
       "      <td>627.000000</td>\n",
       "      <td>627.000000</td>\n",
       "      <td>627.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>29.631308</td>\n",
       "      <td>0.545455</td>\n",
       "      <td>0.379585</td>\n",
       "      <td>34.385399</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>12.511818</td>\n",
       "      <td>1.151090</td>\n",
       "      <td>0.792999</td>\n",
       "      <td>54.597730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.750000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>23.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>7.895800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>28.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>15.045800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>35.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>31.387500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>80.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>512.329200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              age  n_siblings_spouses       parch        fare\n",
       "count  627.000000          627.000000  627.000000  627.000000\n",
       "mean    29.631308            0.545455    0.379585   34.385399\n",
       "std     12.511818            1.151090    0.792999   54.597730\n",
       "min      0.750000            0.000000    0.000000    0.000000\n",
       "25%     23.000000            0.000000    0.000000    7.895800\n",
       "50%     28.000000            0.000000    0.000000   15.045800\n",
       "75%     35.000000            1.000000    0.000000   31.387500\n",
       "max     80.000000            8.000000    5.000000  512.329200"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(627, 9) (264, 9)\n"
     ]
    }
   ],
   "source": [
    "print(train_df.shape, eval_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x12947b550>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAFcBJREFUeJzt3X2MXXd95/H3dxOaUk9lJ032yutkO0FrUgW7GDyiqaBohrTUhIoAqthEEU1KugNSoqWrSK1DV6VdhJR2eWir7tJ1mzRhSz2hhEDWSUtTN9MsqwbwgBs7T40DBjxrbBIShwkoi+l3/7hn2tvp2DP3nvtw5pf3S7qae37nnHs+vvf6M3d+9ykyE0lSuf7VqANIkgbLopekwln0klQ4i16SCmfRS1LhLHpJKpxFL0mFs+glqXAWvSQV7sxRBwA499xzc3x8vKt9nnvuOdatWzeYQDWYq3tNzdbUXNDcbE3NBc3NVifX3Nzck5l53oobZuZpT8AFwH3Aw8BDwLur8XOAe4HHq59nV+MB/B5wCHgQeOVKx9i+fXt267777ut6n2EwV/eamq2puTKbm62puTKbm61OLmBfrtCvmbmqqZuTwA2ZeTFwCXBdRFwM7AT2ZuZmYG+1DPAGYHN1mgY+sopjSJIGZMWiz8yjmfnF6vy3gUeATcDlwG3VZrcBb67OXw58tPqF8wCwISI29j25JGlVIrv49MqIGAfuB7YAX8vMDdV4AE9n5oaI2APclJmfrdbtBX41M/ctuaxp2o/4abVa22dmZroKvrCwwNjYWFf7DIO5utfUbE3NBc3N1tRc0NxsdXJNTU3NZebEihuuZn6n+mUwBswBb62Wn1my/unq5x7gNR3je4GJ0122c/SD19Rcmc3N1tRcmc3N1tRcmc3N1pQ5eiLiRcAdwMcy85PV8LHFKZnq5/FqfJ72E7iLzq/GJEkjsGLRV9MyNwOPZOaHOlbdBVxdnb8a+HTH+C9E2yXAicw82sfMkqQurO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f2WeN8DadBPY0JVeV4e+q00OL9/lR35Yd+bYB+6rb9FPA2U3IRnta5ClgfcdYE3L9JvBodf//n8BZw7if+c5YSSrcWpu6kSR1yaKXpMJZ9JJUOItekgpn0UtS4Sx6SSqcRS9JhbPoJalw/x8MqdwquVi2jAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "train_df.age.hist(bins = 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x12b7337b8>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAD8CAYAAABpcuN4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAADKJJREFUeJzt3H+sZHdZx/HPQ7fdmhaL0IZsWvBS3EhIgbaWiooEEPnRNRQEEyKBkhAaRVFjGi0SSU1FK4iiCUqqYlFREMSAEIJIa0wQW3ftj21tF6pdI7XSIKHUNKlKv/4xZ+F6uffZ7vbunHvr65VMdubM6cwz35vZ954zc1tjjADARh4x9wAAbG1CAUBLKABoCQUALaEAoCUUALSEAoCWUADQEgoAWjvmHmAznHrqqWNlZWXuMQC2lX379n1xjHHa4fZ7WIRiZWUle/funXsMgG2lqv7lwezn1BMALaEAoCUUALSEAoCWUADQEgoAWkIBQEsoAGgJBQAtoQCgJRQAtIQCgJZQANASCgBaQgFASygAaAkFAC2hAKAlFAC0hAKAllAA0BIKAFpCAUBLKABoCQUALaEAoLVj7gE2w/4778nKpR+bewxY18Er9sw9A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8dY3xo2jz72q0311Zat2meLye5Jsl3ZXHa5tD/Jmj1839ttun+U5L8xxJne+F0Km+MMe5P8vuZZ92+J8mLq+pgFqfOn5vkN7LF1i3Z3qH4+yS7p28InJDFhzsfmWuYqjqpqh556HqS5ye5eZrpomm3i5J8eJ4Jk2aWjyR59fSNj2ckuWfVaZalWHMe+KVZrN2h2V4xfePjCUl2J7nuGM5RSX4vya1jjF9bddesa7fRXFth3arqtKp61HT9m5J8fxafoVyT5OXTbmvX7NBavjzJ1dNR2rJmu21V9CuLzwBWr9tS3gtjjDeOMc4YY6xk8ffX1WOMV2YLrNt6w27bSxbfUPhsFudD3zTzLGdm8S2TG5PccmieLM4hfirJ55L8VZJHL2meP8niVMR/Z3Ge87UbzZLFNzzeOa3j/iTnzTDbH07PfVMWb4hdq/Z/0zTbgSQvOsazPTOL00o3Jblhulww99o1c82+bkmemuT6aYabk7x51Xviuiw+SP9Akp3T9hOn27dP9585w2xXT+t2c5I/yte/GbXU98KqOZ+dr3/rafZ1W3vxm9kAtLbzqScAlkAoAGgJBQAtoQCgJRQAtIQCgJZQANASCgBa/ws5zjf/WUrDRgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "train_df.sex.value_counts().plot(kind = 'barh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x12b7a3a90>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAD8CAYAAABkbJM/AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRw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D9zrquqDw/BUzPt82adl3o+rqgeBTwIvYXTI5vivUZqd7/Hsw+0bgAdWOerXmZX7ouHwYFXVY8C7WYY5XwuF8U/A1uGdDKcxOvn04TFnWlCSb0xyxvFt4IeAw4wyXzbsdhnwl+NJ2LJQ1g8DPz+8C+PFwEOzDqFMhDnHal/FaO5hlP01wztfngNsBW5e7XwwehcL8CfA56vqD2bdNPHzvlD2KZn3jUnOHLafCvwgo3MwnwR2DrvNnffj34+dwCeGld+qWiD3F2b94yKMzrvMnvOlvV7GcVZ/uS+Mzvp/kdHxxsvHnWeRrFsYvSvkAHDr8byMjn3+LXA78HHgGePOOuR6P6NDCP/D6FjnLy6UldG7Lq4avg+HgB0TmP3aIdvB4S/Opln7Xz5kvw14xRhzX8DocNNBYP9wuXga5v0E2adh3p8P/POQ8TDwxmF8C6MSuwP4c2D9MP6U4fodw+1bJiz3J4Y5Pwy8j6+9k2rJrxf/p7ckqWUtHJKSJK0CC0OS1GJhSJJaLAxJUouFIUlqsTAkSS0WhiSpxcKQJLX8HzN/2PLgtLgeAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "train_df['class'].value_counts().plot(kind = 'barh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x12b71dda0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZgAAAD8CAYAAABKKbKtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAADUJJREFUeJzt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9KRtGRf408AjwReCwGdx4HKM/kX2T0dnVhhnc+Hng34B7ho8bZnDj7wEbhn23vtA/3Ke1cZtjJx6YMV/HDwyv473D63jEDG4Mo7cb7wfWA+fM2sbh/qXAmklv24nX8Ujg9uF7fQ9wys48vn+TX5LUYqlfg5EkTYmBkSS1MDCSpBYGRpLUwsBIkloYGElSCwMjSWphYCRJLf4Xk05X/eUioq0AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "pd.concat([train_df, y_train], axis = 1).groupby('sex').survived.mean().plot(kind='barh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sex ['male' 'female']\n",
      "n_siblings_spouses [1 0 3 4 2 5 8]\n",
      "parch [0 1 2 5 3 4]\n",
      "class ['Third' 'First' 'Second']\n",
      "deck ['unknown' 'C' 'G' 'A' 'B' 'D' 'F' 'E']\n",
      "embark_town ['Southampton' 'Cherbourg' 'Queenstown' 'unknown']\n",
      "alone ['n' 'y']\n"
     ]
    }
   ],
   "source": [
    "categorical_columns = ['sex', 'n_siblings_spouses', 'parch', 'class',\n",
    "                       'deck', 'embark_town', 'alone']\n",
    "numeric_columns = ['age', 'fare']\n",
    "\n",
    "feature_columns = []\n",
    "for categorical_column in categorical_columns:\n",
    "    vocab = train_df[categorical_column].unique()\n",
    "    print(categorical_column, vocab)\n",
    "    feature_columns.append(\n",
    "        tf.feature_column.indicator_column(\n",
    "            tf.feature_column.categorical_column_with_vocabulary_list(\n",
    "                categorical_column, vocab)))\n",
    "\n",
    "for categorical_column in numeric_columns:\n",
    "    feature_columns.append(\n",
    "        tf.feature_column.numeric_column(\n",
    "            categorical_column, dtype=tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_dataset(data_df, label_df, epochs = 10, shuffle = True,\n",
    "                 batch_size = 32):\n",
    "    dataset = tf.data.Dataset.from_tensor_slices(\n",
    "        (dict(data_df), label_df))\n",
    "    if shuffle:\n",
    "        dataset = dataset.shuffle(10000)\n",
    "    dataset = dataset.repeat(epochs).batch(batch_size)\n",
    "    return dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataset = make_dataset(train_df, y_train, batch_size = 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sex': <tf.Tensor: id=36, shape=(5,), dtype=string, numpy=array([b'male', b'female', b'male', b'male', b'male'], dtype=object)>, 'age': <tf.Tensor: id=28, shape=(5,), dtype=float64, numpy=array([29.,  2., 46., 32., 21.])>, 'n_siblings_spouses': <tf.Tensor: id=34, shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 0], dtype=int32)>, 'parch': <tf.Tensor: id=35, shape=(5,), dtype=int32, numpy=array([0, 1, 0, 0, 0], dtype=int32)>, 'fare': <tf.Tensor: id=33, shape=(5,), dtype=float64, numpy=array([30.   , 26.   , 61.175,  7.925,  8.05 ])>, 'class': <tf.Tensor: id=30, shape=(5,), dtype=string, numpy=array([b'First', b'Second', b'First', b'Third', b'Third'], dtype=object)>, 'deck': <tf.Tensor: id=31, shape=(5,), dtype=string, numpy=array([b'D', b'unknown', b'E', b'unknown', b'unknown'], dtype=object)>, 'embark_town': <tf.Tensor: id=32, shape=(5,), dtype=string, numpy=\n",
      "array([b'Southampton', b'Southampton', b'Southampton', b'Southampton',\n",
      "       b'Southampton'], dtype=object)>, 'alone': <tf.Tensor: id=29, shape=(5,), dtype=string, numpy=array([b'y', b'n', b'n', b'y', b'y'], dtype=object)>} tf.Tensor([0 1 0 0 0], shape=(5,), dtype=int32)\n"
     ]
    }
   ],
   "source": [
    "for x, y in train_dataset.take(1):\n",
    "    print(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W0612 21:28:32.137064 140736297124800 deprecation.py:323] From /Users/zhangyx/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:2758: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.cast` instead.\n",
      "W0612 21:28:32.153759 140736297124800 deprecation.py:323] From /Users/zhangyx/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow/python/ops/lookup_ops.py:1347: to_int64 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.cast` instead.\n",
      "W0612 21:28:32.162392 140736297124800 deprecation.py:323] From /Users/zhangyx/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4307: IndicatorColumn._variable_shape (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.\n",
      "W0612 21:28:32.163794 140736297124800 deprecation.py:323] From /Users/zhangyx/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow/python/feature_column/feature_column_v2.py:4362: VocabularyListCategoricalColumn._num_buckets (from tensorflow.python.feature_column.feature_column_v2) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "The old _FeatureColumn APIs are being deprecated. Please use the new FeatureColumn APIs instead.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[29.]\n",
      " [ 2.]\n",
      " [46.]\n",
      " [32.]\n",
      " [21.]]\n",
      "[[1. 0.]\n",
      " [0. 1.]\n",
      " [1. 0.]\n",
      " [1. 0.]\n",
      " [1. 0.]]\n"
     ]
    }
   ],
   "source": [
    "# keras.layers.DenseFeature\n",
    "for x, y in train_dataset.take(1):\n",
    "    age_column = feature_columns[7]\n",
    "    gender_column = feature_columns[0]\n",
    "    print(keras.layers.DenseFeatures(age_column)(x).numpy())\n",
    "    print(keras.layers.DenseFeatures(gender_column)(x).numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[29.     0.     1.     0.     1.     0.     0.     0.     0.     0.\n",
      "   0.     1.     0.     0.     1.     0.     0.     0.    30.     0.\n",
      "   1.     0.     0.     0.     0.     0.     1.     0.     0.     0.\n",
      "   0.     0.     1.     0.   ]\n",
      " [ 2.     1.     0.     0.     0.     1.     1.     0.     0.     0.\n",
      "   0.     0.     0.     0.     1.     0.     0.     0.    26.     1.\n",
      "   0.     0.     0.     0.     0.     0.     0.     1.     0.     0.\n",
      "   0.     0.     0.     1.   ]\n",
      " [46.     1.     0.     0.     1.     0.     0.     0.     0.     0.\n",
      "   0.     0.     0.     1.     1.     0.     0.     0.    61.175  1.\n",
      "   0.     0.     0.     0.     0.     0.     1.     0.     0.     0.\n",
      "   0.     0.     1.     0.   ]\n",
      " [32.     0.     1.     1.     0.     0.     1.     0.     0.     0.\n",
      "   0.     0.     0.     0.     1.     0.     0.     0.     7.925  0.\n",
      "   1.     0.     0.     0.     0.     0.     1.     0.     0.     0.\n",
      "   0.     0.     1.     0.   ]\n",
      " [21.     0.     1.     1.     0.     0.     1.     0.     0.     0.\n",
      "   0.     0.     0.     0.     1.     0.     0.     0.     8.05   0.\n",
      "   1.     0.     0.     0.     0.     0.     1.     0.     0.     0.\n",
      "   0.     0.     1.     0.   ]]\n"
     ]
    }
   ],
   "source": [
    "# keras.layers.DenseFeature\n",
    "for x, y in train_dataset.take(1):\n",
    "    print(keras.layers.DenseFeatures(feature_columns)(x).numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = keras.models.Sequential([\n",
    "    keras.layers.DenseFeatures(feature_columns),\n",
    "    keras.layers.Dense(100, activation='relu'),\n",
    "    keras.layers.Dense(100, activation='relu'),\n",
    "    keras.layers.Dense(2, activation='softmax'),\n",
    "])\n",
    "model.compile(loss='sparse_categorical_crossentropy',\n",
    "              optimizer = keras.optimizers.SGD(lr=0.01),\n",
    "              metrics = ['accuracy'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "20/20 [==============================] - 1s 60ms/step - loss: 1.7809 - accuracy: 0.5523 - val_loss: 0.8140 - val_accuracy: 0.6992\n",
      "Epoch 2/100\n",
      "20/20 [==============================] - 1s 32ms/step - loss: 0.7985 - accuracy: 0.6325 - val_loss: 0.6381 - val_accuracy: 0.6875\n",
      "Epoch 3/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.7483 - accuracy: 0.6304 - val_loss: 0.6306 - val_accuracy: 0.6406\n",
      "Epoch 4/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.6461 - accuracy: 0.6396 - val_loss: 0.5957 - val_accuracy: 0.7031\n",
      "Epoch 5/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.6153 - accuracy: 0.6736 - val_loss: 0.5827 - val_accuracy: 0.6992\n",
      "Epoch 6/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.6233 - accuracy: 0.6573 - val_loss: 0.6350 - val_accuracy: 0.6992\n",
      "Epoch 7/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.6138 - accuracy: 0.6900 - val_loss: 0.6320 - val_accuracy: 0.6250\n",
      "Epoch 8/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.6053 - accuracy: 0.7124 - val_loss: 0.5757 - val_accuracy: 0.7148\n",
      "Epoch 9/100\n",
      "20/20 [==============================] - 1s 32ms/step - loss: 0.5748 - accuracy: 0.6971 - val_loss: 0.6258 - val_accuracy: 0.6602\n",
      "Epoch 10/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.6086 - accuracy: 0.6659 - val_loss: 0.5901 - val_accuracy: 0.6797\n",
      "Epoch 11/100\n",
      "20/20 [==============================] - 1s 32ms/step - loss: 0.5976 - accuracy: 0.6653 - val_loss: 0.5943 - val_accuracy: 0.6914\n",
      "Epoch 12/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.6003 - accuracy: 0.6745 - val_loss: 0.5728 - val_accuracy: 0.6875\n",
      "Epoch 13/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5726 - accuracy: 0.7267 - val_loss: 0.5710 - val_accuracy: 0.7109\n",
      "Epoch 14/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5714 - accuracy: 0.6819 - val_loss: 0.6088 - val_accuracy: 0.6953\n",
      "Epoch 15/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5967 - accuracy: 0.7044 - val_loss: 0.5612 - val_accuracy: 0.6992\n",
      "Epoch 16/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.5761 - accuracy: 0.7188 - val_loss: 0.5598 - val_accuracy: 0.6914\n",
      "Epoch 17/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.5970 - accuracy: 0.6734 - val_loss: 0.5909 - val_accuracy: 0.6758\n",
      "Epoch 18/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5932 - accuracy: 0.6634 - val_loss: 0.5557 - val_accuracy: 0.7109\n",
      "Epoch 19/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5564 - accuracy: 0.7147 - val_loss: 0.5954 - val_accuracy: 0.6836\n",
      "Epoch 20/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.6084 - accuracy: 0.6774 - val_loss: 0.5591 - val_accuracy: 0.7109\n",
      "Epoch 21/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5602 - accuracy: 0.7173 - val_loss: 0.6237 - val_accuracy: 0.6797\n",
      "Epoch 22/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5751 - accuracy: 0.6548 - val_loss: 0.5580 - val_accuracy: 0.6758\n",
      "Epoch 23/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5888 - accuracy: 0.6923 - val_loss: 0.5597 - val_accuracy: 0.6914\n",
      "Epoch 24/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5619 - accuracy: 0.7202 - val_loss: 0.6134 - val_accuracy: 0.7148\n",
      "Epoch 25/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5686 - accuracy: 0.7417 - val_loss: 0.5546 - val_accuracy: 0.6758\n",
      "Epoch 26/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5640 - accuracy: 0.6975 - val_loss: 0.5496 - val_accuracy: 0.7031\n",
      "Epoch 27/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5508 - accuracy: 0.7105 - val_loss: 0.5756 - val_accuracy: 0.7070\n",
      "Epoch 28/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5718 - accuracy: 0.7295 - val_loss: 0.5654 - val_accuracy: 0.6641\n",
      "Epoch 29/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5402 - accuracy: 0.7065 - val_loss: 0.5418 - val_accuracy: 0.7188\n",
      "Epoch 30/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5782 - accuracy: 0.6677 - val_loss: 0.5777 - val_accuracy: 0.6875\n",
      "Epoch 31/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5539 - accuracy: 0.7070 - val_loss: 0.5489 - val_accuracy: 0.6992\n",
      "Epoch 32/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5668 - accuracy: 0.7050 - val_loss: 0.5567 - val_accuracy: 0.6992\n",
      "Epoch 33/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5748 - accuracy: 0.7277 - val_loss: 0.5341 - val_accuracy: 0.6992\n",
      "Epoch 34/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.5507 - accuracy: 0.7717 - val_loss: 0.6581 - val_accuracy: 0.6992\n",
      "Epoch 35/100\n",
      "20/20 [==============================] - 1s 49ms/step - loss: 0.5614 - accuracy: 0.7133 - val_loss: 0.5472 - val_accuracy: 0.6992\n",
      "Epoch 36/100\n",
      "20/20 [==============================] - 1s 42ms/step - loss: 0.5841 - accuracy: 0.6934 - val_loss: 0.5718 - val_accuracy: 0.6875\n",
      "Epoch 37/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5605 - accuracy: 0.7094 - val_loss: 0.5569 - val_accuracy: 0.6992\n",
      "Epoch 38/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.5211 - accuracy: 0.7542 - val_loss: 0.5451 - val_accuracy: 0.7188\n",
      "Epoch 39/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5285 - accuracy: 0.7782 - val_loss: 0.5400 - val_accuracy: 0.7188\n",
      "Epoch 40/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5467 - accuracy: 0.7655 - val_loss: 0.5744 - val_accuracy: 0.7070\n",
      "Epoch 41/100\n",
      "20/20 [==============================] - 1s 40ms/step - loss: 0.5411 - accuracy: 0.7286 - val_loss: 0.5194 - val_accuracy: 0.7344\n",
      "Epoch 42/100\n",
      "20/20 [==============================] - 1s 40ms/step - loss: 0.5750 - accuracy: 0.7327 - val_loss: 0.5291 - val_accuracy: 0.7344\n",
      "Epoch 43/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5450 - accuracy: 0.7169 - val_loss: 0.5300 - val_accuracy: 0.7109\n",
      "Epoch 44/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5367 - accuracy: 0.7269 - val_loss: 0.5178 - val_accuracy: 0.7266\n",
      "Epoch 45/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.5188 - accuracy: 0.7521 - val_loss: 0.5302 - val_accuracy: 0.7305\n",
      "Epoch 46/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5149 - accuracy: 0.7772 - val_loss: 0.5339 - val_accuracy: 0.7148\n",
      "Epoch 47/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5279 - accuracy: 0.7138 - val_loss: 0.6898 - val_accuracy: 0.6250\n",
      "Epoch 48/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5596 - accuracy: 0.6796 - val_loss: 0.5140 - val_accuracy: 0.7305\n",
      "Epoch 49/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.5711 - accuracy: 0.7214 - val_loss: 0.5213 - val_accuracy: 0.7383\n",
      "Epoch 50/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.5384 - accuracy: 0.7600 - val_loss: 0.5173 - val_accuracy: 0.7148\n",
      "Epoch 51/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5576 - accuracy: 0.7558 - val_loss: 0.7255 - val_accuracy: 0.5977\n",
      "Epoch 52/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5203 - accuracy: 0.7395 - val_loss: 0.5395 - val_accuracy: 0.7344\n",
      "Epoch 53/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.5095 - accuracy: 0.7313 - val_loss: 0.5255 - val_accuracy: 0.7188\n",
      "Epoch 54/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.5455 - accuracy: 0.7134 - val_loss: 0.5464 - val_accuracy: 0.7188\n",
      "Epoch 55/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5179 - accuracy: 0.7379 - val_loss: 0.5432 - val_accuracy: 0.7188\n",
      "Epoch 56/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.5236 - accuracy: 0.7619 - val_loss: 0.5835 - val_accuracy: 0.7109\n",
      "Epoch 57/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5367 - accuracy: 0.7311 - val_loss: 0.5632 - val_accuracy: 0.6914\n",
      "Epoch 58/100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20/20 [==============================] - 1s 32ms/step - loss: 0.5607 - accuracy: 0.7104 - val_loss: 0.5422 - val_accuracy: 0.7188\n",
      "Epoch 59/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5511 - accuracy: 0.7216 - val_loss: 0.5191 - val_accuracy: 0.7344\n",
      "Epoch 60/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5082 - accuracy: 0.7337 - val_loss: 0.4986 - val_accuracy: 0.7539\n",
      "Epoch 61/100\n",
      "20/20 [==============================] - 1s 44ms/step - loss: 0.5041 - accuracy: 0.7682 - val_loss: 0.5438 - val_accuracy: 0.7305\n",
      "Epoch 62/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.5568 - accuracy: 0.7381 - val_loss: 0.5093 - val_accuracy: 0.7227\n",
      "Epoch 63/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.5046 - accuracy: 0.7889 - val_loss: 0.5182 - val_accuracy: 0.7656\n",
      "Epoch 64/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5458 - accuracy: 0.6970 - val_loss: 0.5954 - val_accuracy: 0.7031\n",
      "Epoch 65/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5215 - accuracy: 0.7386 - val_loss: 0.5548 - val_accuracy: 0.7188\n",
      "Epoch 66/100\n",
      "20/20 [==============================] - 1s 38ms/step - loss: 0.5261 - accuracy: 0.7392 - val_loss: 0.5353 - val_accuracy: 0.7344\n",
      "Epoch 67/100\n",
      "20/20 [==============================] - 1s 43ms/step - loss: 0.5046 - accuracy: 0.7826 - val_loss: 0.5257 - val_accuracy: 0.7383\n",
      "Epoch 68/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.5524 - accuracy: 0.6854 - val_loss: 0.5303 - val_accuracy: 0.7188\n",
      "Epoch 69/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.5382 - accuracy: 0.7314 - val_loss: 0.5192 - val_accuracy: 0.7617\n",
      "Epoch 70/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5301 - accuracy: 0.7505 - val_loss: 0.5006 - val_accuracy: 0.7539\n",
      "Epoch 71/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5103 - accuracy: 0.7618 - val_loss: 0.5025 - val_accuracy: 0.7461\n",
      "Epoch 72/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5173 - accuracy: 0.7439 - val_loss: 0.5447 - val_accuracy: 0.7148\n",
      "Epoch 73/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5584 - accuracy: 0.7460 - val_loss: 0.5098 - val_accuracy: 0.7383\n",
      "Epoch 74/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5037 - accuracy: 0.7842 - val_loss: 0.5262 - val_accuracy: 0.7461\n",
      "Epoch 75/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.5004 - accuracy: 0.7716 - val_loss: 0.5730 - val_accuracy: 0.7148\n",
      "Epoch 76/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5641 - accuracy: 0.7452 - val_loss: 0.5085 - val_accuracy: 0.7617\n",
      "Epoch 77/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5091 - accuracy: 0.7811 - val_loss: 0.5053 - val_accuracy: 0.7344\n",
      "Epoch 78/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5511 - accuracy: 0.7187 - val_loss: 0.5031 - val_accuracy: 0.7266\n",
      "Epoch 79/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5083 - accuracy: 0.7593 - val_loss: 0.4989 - val_accuracy: 0.7617\n",
      "Epoch 80/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.4995 - accuracy: 0.7893 - val_loss: 0.5074 - val_accuracy: 0.7617\n",
      "Epoch 81/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5241 - accuracy: 0.7507 - val_loss: 0.4950 - val_accuracy: 0.7383\n",
      "Epoch 82/100\n",
      "20/20 [==============================] - 1s 42ms/step - loss: 0.4666 - accuracy: 0.7729 - val_loss: 0.5883 - val_accuracy: 0.6836\n",
      "Epoch 83/100\n",
      "20/20 [==============================] - 1s 37ms/step - loss: 0.5805 - accuracy: 0.7085 - val_loss: 0.5400 - val_accuracy: 0.7188\n",
      "Epoch 84/100\n",
      "20/20 [==============================] - 1s 34ms/step - loss: 0.5233 - accuracy: 0.7658 - val_loss: 0.4943 - val_accuracy: 0.7422\n",
      "Epoch 85/100\n",
      "20/20 [==============================] - 1s 32ms/step - loss: 0.4887 - accuracy: 0.8143 - val_loss: 0.6352 - val_accuracy: 0.6641\n",
      "Epoch 86/100\n",
      "20/20 [==============================] - 1s 45ms/step - loss: 0.5065 - accuracy: 0.7391 - val_loss: 0.4998 - val_accuracy: 0.7305\n",
      "Epoch 87/100\n",
      "20/20 [==============================] - 1s 45ms/step - loss: 0.5476 - accuracy: 0.7277 - val_loss: 0.5095 - val_accuracy: 0.7422\n",
      "Epoch 88/100\n",
      "20/20 [==============================] - 1s 60ms/step - loss: 0.5392 - accuracy: 0.7086 - val_loss: 0.5002 - val_accuracy: 0.7422\n",
      "Epoch 89/100\n",
      "20/20 [==============================] - 1s 45ms/step - loss: 0.5009 - accuracy: 0.7780 - val_loss: 0.5163 - val_accuracy: 0.7305\n",
      "Epoch 90/100\n",
      "20/20 [==============================] - 1s 41ms/step - loss: 0.4921 - accuracy: 0.7718 - val_loss: 0.5276 - val_accuracy: 0.7344\n",
      "Epoch 91/100\n",
      "20/20 [==============================] - 1s 39ms/step - loss: 0.5365 - accuracy: 0.7000 - val_loss: 0.5515 - val_accuracy: 0.6914\n",
      "Epoch 92/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.4888 - accuracy: 0.7715 - val_loss: 0.4882 - val_accuracy: 0.7461\n",
      "Epoch 93/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.5225 - accuracy: 0.7628 - val_loss: 0.5469 - val_accuracy: 0.7305\n",
      "Epoch 94/100\n",
      "20/20 [==============================] - 1s 33ms/step - loss: 0.4994 - accuracy: 0.7591 - val_loss: 0.5015 - val_accuracy: 0.7539\n",
      "Epoch 95/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5099 - accuracy: 0.7522 - val_loss: 0.5085 - val_accuracy: 0.7578\n",
      "Epoch 96/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.4955 - accuracy: 0.7732 - val_loss: 0.4935 - val_accuracy: 0.7422\n",
      "Epoch 97/100\n",
      "20/20 [==============================] - 1s 35ms/step - loss: 0.5200 - accuracy: 0.7628 - val_loss: 0.4933 - val_accuracy: 0.7461\n",
      "Epoch 98/100\n",
      "20/20 [==============================] - 1s 36ms/step - loss: 0.4800 - accuracy: 0.7610 - val_loss: 0.6802 - val_accuracy: 0.6641\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W0612 21:29:53.941813 140736297124800 training_generator.py:228] Your dataset ran out of data; interrupting training. Make sure that your dataset can generate at least `steps_per_epoch * epochs` batches (in this case, 2000 batches). You may need to use the repeat() function when building your dataset.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 99/100\n",
      "\r",
      " 0/20 [..............................] - ETA: 0s - loss: 0.0000e+00 - accuracy: 0.0000e+00 - val_loss: 0.0000e+00 - val_accuracy: 0.0000e+00"
     ]
    }
   ],
   "source": [
    "# 1. model.fit \n",
    "# 2. model -> estimator -> train\n",
    "\n",
    "train_dataset = make_dataset(train_df, y_train, epochs = 100)\n",
    "eval_dataset = make_dataset(eval_df, y_eval, epochs = 1, shuffle = False)\n",
    "history = model.fit(train_dataset,\n",
    "                    validation_data = eval_dataset,\n",
    "                    steps_per_epoch = 20,\n",
    "                    validation_steps = 8,\n",
    "                    epochs = 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W0612 21:31:02.642828 140736297124800 estimator.py:1799] Using temporary folder as model directory: /var/folders/9t/8dd6n43j7zj6gxv762vzwm2h0000gn/T/tmp_uxfsac1\n",
      "W0612 21:31:02.645179 140736297124800 keras.py:492] You are creating an Estimator from a Keras model manually subclassed from `Model`, that was already called on some inputs (and thus already had weights). We are currently unable to preserve the model's state (its weights) as part of the estimator in this case. Be warned that the estimator has been created using a freshly initialized version of your model.\n",
      "Note that this doesn't affect the state of the model instance you passed as `keras_model` argument.\n",
      "W0612 21:31:02.658140 140736297124800 deprecation.py:323] From /Users/zhangyx/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow/python/training/training_util.py:238: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "\"The dictionary passed into features does not have the expected inputs keys defined in the keras model.\\n\\tExpected keys: {'input_9', 'input_1', 'input_8', 'input_6', 'input_4', 'input_5', 'input_3', 'input_2', 'input_7'}\\n\\tfeatures keys: {'age', 'sex', 'embark_town', 'alone', 'deck', 'n_siblings_spouses', 'class', 'parch', 'fare'}\\n\\tDifference: {'input_9', 'age', 'input_1', 'input_8', 'sex', 'embark_town', 'alone', 'input_6', 'deck', 'input_4', 'n_siblings_spouses', 'class', 'input_5', 'parch', 'input_3', 'input_2', 'input_7', 'fare'}\"",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-18-7904a567a5dd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;31m# 1. function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;31m# 2. return a. (features, labels) b. dataset -> (feature, label)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m estimator.train(input_fn = lambda : make_dataset(\n\u001b[0m\u001b[1;32m      5\u001b[0m     train_df, y_train, epochs=100))\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(self, input_fn, hooks, steps, max_steps, saving_listeners)\u001b[0m\n\u001b[1;32m    357\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    358\u001b[0m       \u001b[0msaving_listeners\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_check_listeners_type\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msaving_listeners\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 359\u001b[0;31m       \u001b[0mloss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_train_model\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhooks\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msaving_listeners\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    360\u001b[0m       \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Loss for final step: %s.'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    361\u001b[0m       \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py\u001b[0m in \u001b[0;36m_train_model\u001b[0;34m(self, input_fn, hooks, saving_listeners)\u001b[0m\n\u001b[1;32m   1137\u001b[0m       \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_train_model_distributed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhooks\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msaving_listeners\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1138\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1139\u001b[0;31m       \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_train_model_default\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhooks\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msaving_listeners\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1140\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1141\u001b[0m   \u001b[0;32mdef\u001b[0m \u001b[0m_train_model_default\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_fn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhooks\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msaving_listeners\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py\u001b[0m in \u001b[0;36m_train_model_default\u001b[0;34m(self, input_fn, hooks, saving_listeners)\u001b[0m\n\u001b[1;32m   1167\u001b[0m       \u001b[0mworker_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_hooks\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1168\u001b[0m       estimator_spec = self._call_model_fn(\n\u001b[0;32m-> 1169\u001b[0;31m           features, labels, ModeKeys.TRAIN, self.config)\n\u001b[0m\u001b[1;32m   1170\u001b[0m       \u001b[0mglobal_step_tensor\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtraining_util\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_global_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1171\u001b[0m       return self._train_with_estimator_spec(estimator_spec, worker_hooks,\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/estimator.py\u001b[0m in \u001b[0;36m_call_model_fn\u001b[0;34m(self, features, labels, mode, config)\u001b[0m\n\u001b[1;32m   1125\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1126\u001b[0m     \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Calling model_fn.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1127\u001b[0;31m     \u001b[0mmodel_fn_results\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_model_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfeatures\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfeatures\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1128\u001b[0m     \u001b[0mlogging\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minfo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Done calling model_fn.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1129\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/keras.py\u001b[0m in \u001b[0;36mmodel_fn\u001b[0;34m(features, labels, mode)\u001b[0m\n\u001b[1;32m    280\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    281\u001b[0m     model = _clone_and_build_model(mode, keras_model, custom_objects, features,\n\u001b[0;32m--> 282\u001b[0;31m                                    labels)\n\u001b[0m\u001b[1;32m    283\u001b[0m     \u001b[0mmodel_output_names\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    284\u001b[0m     \u001b[0;31m# We need to make sure that the output names of the last layer in the model\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/keras.py\u001b[0m in \u001b[0;36m_clone_and_build_model\u001b[0;34m(mode, keras_model, custom_objects, features, labels)\u001b[0m\n\u001b[1;32m    186\u001b[0m   \u001b[0mK\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_learning_phase\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mModeKeys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTRAIN\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    187\u001b[0m   input_tensors, target_tensors = _convert_estimator_io_to_keras(\n\u001b[0;32m--> 188\u001b[0;31m       keras_model, features, labels)\n\u001b[0m\u001b[1;32m    189\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    190\u001b[0m   \u001b[0mcompile_clone\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mModeKeys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mPREDICT\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/keras.py\u001b[0m in \u001b[0;36m_convert_estimator_io_to_keras\u001b[0;34m(keras_model, features, labels)\u001b[0m\n\u001b[1;32m    159\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    160\u001b[0m   input_tensors = _to_ordered_tensor_list(\n\u001b[0;32m--> 161\u001b[0;31m       features, input_names, 'features', 'inputs')\n\u001b[0m\u001b[1;32m    162\u001b[0m   target_tensors = _to_ordered_tensor_list(\n\u001b[1;32m    163\u001b[0m       labels, output_names, 'labels', 'outputs')\n",
      "\u001b[0;32m~/workspace/environments/tf2_py3/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/keras.py\u001b[0m in \u001b[0;36m_to_ordered_tensor_list\u001b[0;34m(obj, key_order, obj_name, order_name)\u001b[0m\n\u001b[1;32m    141\u001b[0m                 \u001b[0morder_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0morder_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0morder_keys\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey_order\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    142\u001b[0m                 \u001b[0mobj_name\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mobj_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mobj_keys\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 143\u001b[0;31m                 different_keys=different_keys))\n\u001b[0m\u001b[1;32m    144\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    145\u001b[0m       \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0m_convert_tensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mkey_order\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: \"The dictionary passed into features does not have the expected inputs keys defined in the keras model.\\n\\tExpected keys: {'input_9', 'input_1', 'input_8', 'input_6', 'input_4', 'input_5', 'input_3', 'input_2', 'input_7'}\\n\\tfeatures keys: {'age', 'sex', 'embark_town', 'alone', 'deck', 'n_siblings_spouses', 'class', 'parch', 'fare'}\\n\\tDifference: {'input_9', 'age', 'input_1', 'input_8', 'sex', 'embark_town', 'alone', 'input_6', 'deck', 'input_4', 'n_siblings_spouses', 'class', 'input_5', 'parch', 'input_3', 'input_2', 'input_7', 'fare'}\""
     ]
    }
   ],
   "source": [
    "estimator = keras.estimator.model_to_estimator(model)\n",
    "# 1. function\n",
    "# 2. return a. (features, labels) b. dataset -> (feature, label)\n",
    "estimator.train(input_fn = lambda : make_dataset(\n",
    "    train_df, y_train, epochs=100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
